home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d18 / turbcomm.arc / COMMLIB.INC < prev    next >
Text File  |  1989-06-30  |  12KB  |  368 lines

  1. { File: COMMLIB.INC  -  MS-DOS Version for Ver 2.05+  -  3/9/85
  2.    Function: Provide a library of functions needed for communications
  3.             on a DEC Rainbow.
  4.  
  5.    Written by: Stew Stryker                                    }
  6.  
  7.  
  8.    Const
  9.       Successful = 255;
  10.       Unsuccessful = 0;
  11.  
  12.    Type
  13.       { This is used when changing comm port parameters }
  14.       __CommCtrlBlock = RECORD
  15.                         ModemCtrl, StopBits, DataBits,
  16.                         Parity, RCVBaud, XMTBaud,XONChar,
  17.                         XOFFChar, RCVXON, XMTXON : Byte;
  18.                         AltBufSize, BuffAddrOffset, BuffAddrSeg : INTEGER;
  19.                         END;
  20.                       
  21.       { This is used for all comm port I/O }
  22.       __IOCTLPacketType = RECORD
  23.                           Fnction, Func_retC : Byte;
  24.                           Character : Char;
  25.                           Char_Stat : Byte;
  26.                           Buffer: Byte;    { either CCB or 0 }
  27.                           end;
  28.  
  29.                          
  30.       CommInType = RECORD         { With each character read in, there's }
  31.                    Char : Char;   { the character itself, and a byte }
  32.                    Error : Byte;  { indicating any data errors }
  33.                    end;
  34.                    
  35.  
  36.       { These are the extended communications functions }
  37.       __SubFunction = ( ProgramDev, ProgramDefault, SetDefBuf,
  38.                       ReadDevSetup, EnableRecvIntr, DisableRecvIntr,
  39.                       ReadInStat, ReadChar, GetChar, ReadOutStat,
  40.                       WriteChar, PutChar, XMTImmed, ReadModem,
  41.                       SetModem, XMTBreak, StopBreak, SetRecvIntr,
  42.                       ResetRecvIntr, EmptyBufIntr, ResetEmptyBufIntr,
  43.                       SetIntrRoutine, ResetDevIntr );
  44.  
  45.       ParityType = ( NoChange, Even, Odd, None );  { Possible parity values }
  46.       DataBitsType = (DBNoChange, DB5, DB6, DB7, DB8, DB7S, DB7M );
  47.  
  48.    Var
  49.       __Buffer1 : String[255];     { The next 4 lines set up communications }
  50.       __Buffer2 : String[255];     { data buffer of 1024 bytes }
  51.       __Buffer3 : String[255];
  52.       __Buffer4 : String[255];
  53.       DefaultBaud : Integer;
  54.       DefaultDataBits : DataBitsType;
  55.       DefaultParity : ParityType;
  56.       DefaultStopBits : Real;
  57.       CommIn : CommInType;
  58.       __SetSerialIO : __IOCTLPacketType;  { These two variables must be }
  59.       __CCB : __CommCtrlBlock;            { defined contiguously.       }
  60.       __IOCTLPacket : __IOCTLPacketType;
  61.  
  62.  
  63.    Function __CallDOSIO(PortNumber : Byte;
  64.                          Command : __Subfunction; CharOut : Char;
  65.                          BufferOut : Byte) : Boolean;
  66.    { Do an MS-DOS Communications Driver Interrupt }
  67.  
  68.  
  69.  
  70.       Begin                         { First set up IO Control Packet }
  71.  
  72.          With __IOCTLPacket Do Begin
  73.             Fnction := Ord(Command);
  74.             Buffer := BufferOut;
  75.             Character := CharOut;
  76.             end;
  77.  
  78.          With __Registers Do
  79.             Begin
  80.                AH := $44;
  81.                AL := $02;
  82.                BX := PortNumber;
  83.                DS := Seg(__IOCTLPacket);
  84.                DX := Ofs(__IOCTLPacket);
  85.                end;
  86.          Intr($21,__Registers);
  87.  
  88.          If __IOCTLPacket.Func_retC = Successful 
  89.             Then __CallDOSIO := True               { This returns whether or }
  90.             Else __CallDOSIO := False;             { not the function was }
  91.                                                    { successful }
  92.          end; { __CallDOSIO }
  93.  
  94.  
  95.    Function SetSerial(PortNumber : Byte; BaudRate : Integer;
  96.                        StopBitsIn : Real; DataBitsIn : DataBitsType;
  97.                        ParityIn : ParityType; CharOut : Char) : Boolean;
  98.    { Set serial parameters for a COM port }
  99.  
  100.  
  101.    Begin
  102.  
  103.       With __Registers Do
  104.          Begin
  105.             AH := $44;
  106.             AL := 2;
  107.             BX := PortNumber;
  108.             DS := Seg(__SetSerialIO);
  109.             DX := Ofs(__SetSerialIO);
  110.             end;
  111.  
  112.          __SetSerialIO.Fnction := Ord(ProgramDev);
  113.          __SetSerialIO.Buffer  := 1;
  114.  
  115.       With __CCB Do
  116.          Begin
  117.             ModemCtrl := 1;
  118.             If StopBitsIn = 2 Then StopBits := 3
  119.                Else If StopBitsIn = 1.5 Then StopBits := 2
  120.                   Else If StopBitsIn = 1 Then StopBits := 1
  121.                      Else StopBits := 0;
  122.             DataBits := Ord(DataBitsIn);
  123.             Parity := Ord(ParityIn);
  124.             Case BaudRate of
  125.                110:  RCVBaud := 3;
  126.                150:  RCVBaud := 5;
  127.                300:  RCVBaud := 7;
  128.                600:  RCVBaud := 8;
  129.                1200: RCVBaud := 9;
  130.                2400: RCVBaud := 12;
  131.                4800: RCVBaud := 14;
  132.                else RCVBaud := 16;     { Default to 9600 Baud }
  133.                end;                    { Case of baud rate }
  134.             XMTBaud  := RCVBaud;       { Transmit and Receive Rates are same }
  135.             XONChar  := 17;            { ^Q }
  136.             XOFFChar := 19;            { ^S }
  137.             RCVXON   := 2;       { Always set auto XON/XOFF on }
  138.             XMTXON   := 2;       { The manual says this will turn XON/XOFF }
  139.                                  { off.  It's wrong.  Try it if you want.  }
  140.             AltBufSize := 1024;         { Set up 1024 byte buffer }
  141.             BuffAddrOffset := Ofs(__Buffer1);
  142.             BuffAddrSeg    := Seg(__Buffer1);
  143.             end;
  144.  
  145.       Intr($21,__Registers);
  146.  
  147.       If __SetSerialIO.Func_retC = Successful
  148.          Then SetSerial := True
  149.          Else SetSerial := False;
  150.                
  151.       end; { SetSerial }
  152.  
  153.  
  154.    Function ReadCurrentCommSettings : Boolean;
  155.       { Read the current Comm Setup values }
  156.  
  157.    Begin
  158.  
  159.       { Get the values }
  160.       With __Registers Do
  161.          Begin
  162.             AH := $44;
  163.             AL := 2;
  164.             BX := 3;
  165.             DS := Seg(__SetSerialIO);
  166.             DX := Ofs(__SetSerialIO);
  167.             end;
  168.  
  169.          __SetSerialIO.Fnction := Ord(ReadDevSetup);
  170.          __SetSerialIO.Buffer  := 1;
  171.          Intr($21,__Registers);
  172.  
  173.       If __SetSerialIO.Func_retC = Successful Then
  174.          Begin
  175.             ReadCurrentCommSettings := True;
  176.  
  177.             { Now decode and save default values }
  178.             Case __CCB.RCVBaud Of
  179.                3: DefaultBaud := 110;
  180.                5: DefaultBaud := 150;
  181.                7: DefaultBaud := 300;
  182.                8: DefaultBaud := 600;
  183.                9: DefaultBaud := 1200;
  184.                12: DefaultBaud := 2400;
  185.                14: DefaultBaud := 4800;
  186.                16: DefaultBaud := 9600;
  187.                End; { Case of __CCB.RCVBaud }
  188.  
  189.             DefaultDataBits := DataBitsType(__CCB.DataBits);
  190.             DefaultParity := ParityType(__CCB.Parity);
  191.  
  192.             Case __CCB.StopBits Of
  193.                3: DefaultStopBits := 2;
  194.                2: DefaultStopBits := 1.5;
  195.                1: DefaultStopBits := 1;
  196.                0: DefaultStopBits := 0;
  197.                End; { Case statement }
  198.  
  199.             End { if successful }
  200.          Else
  201.             ReadCurrentCommSettings := False;
  202.  
  203.       End; { Function ReadCurrentCommSettings }
  204.  
  205.  
  206.  
  207.  
  208.    Function ReplaceDefault : Boolean;
  209.       { Replace current comm settings with default values }
  210.  
  211.       Var
  212.          TempStatus : Boolean;
  213.  
  214.       Begin
  215.  
  216.          { Note:  Since we're getting many functions status, we'll use }
  217.          { an artificial method to determine the total status.  With   }
  218.          { this method, if one function call is unsuccessful, TempStatus }
  219.          { becomes, and stays unsuccessful.  Sort of like a master AND. }
  220.  
  221.  
  222.          { Disable Receiver Interrupts }
  223.          TempStatus := __CallDOSIO(3, DisableRecvIntr, Chr(0), 1);
  224.  
  225.          { Next, Set device to use default buffer }
  226.          TempStatus := TempStatus And __CallDOSIO(3, SetDefBuf, Chr(0), 1);
  227.  
  228.          { Last, program comm to default settings }
  229.          TempStatus := TempStatus And __CallDOSIO(3, ProgramDefault, Chr(0), 1);
  230.          
  231.          { Now save final status }
  232.          ReplaceDefault := TempStatus;
  233.  
  234.          end; { ReplaceDefault }
  235.  
  236.  
  237.    Function DisconnectModem : Boolean;
  238.       { Turn off DSR modem signal and reset it }
  239.       Begin
  240.          DisconnectModem := __CallDOSIO(3, SetModem, Chr(0), 1);
  241.          End; { Function DisconnectModem }
  242.  
  243.  
  244.    Function CheckInputStatus : Boolean;
  245.       { Check the Input line Status of the comm port  }
  246.       
  247.       Begin
  248.          CheckInputStatus := __CallDOSIO(3, ReadInStat, Chr(0), 1);
  249.          end; { CheckInputStatus }
  250.  
  251.  
  252.    Function ReadCommChar : Boolean;
  253.       { Read a character in from the comm port }
  254.  
  255.       Begin
  256.          ReadCommChar := __CallDOSIO(3, ReadChar, Chr(0), 1);
  257.  
  258.          { If any character is returned, save it and any data errors }
  259.          With CommIn Do Begin
  260.             Char := __IOCTLPacket.Character;
  261.             Error := __IOCTLPacket.Char_Stat;
  262.                { I ignore the error byte.  If Error > 0 => an error occurred }
  263.             end;
  264.  
  265.          end; { ReadCommChar }
  266.                               
  267.  
  268.    Procedure GetCommChar;
  269.       { Go for a character from the comm port, don't come back til you get it }
  270.       { Note: This procedure is dangerous, just because it can wait forever }
  271.       {       for a character to come in. }
  272.  
  273.       Begin
  274.          DummyLogical := __CallDOSIO(3,GetChar, Chr(0), 1);
  275.  
  276.          { When character is returned, save it and any data errors }
  277.          With CommIn Do Begin
  278.             Char := __IOCTLPacket.Character;
  279.             Error := __IOCTLPacket.Char_Stat;
  280.             end;
  281.  
  282.          End;
  283.  
  284.  
  285.    Function CheckOutputStatus : Boolean;
  286.       { Check whether it's ok to send a character out the comm port }
  287.       
  288.       Begin
  289.          CheckOutputStatus := __CallDOSIO(3, ReadOutStat, Chr(0), 1);
  290.          end; { Function CheckOutputStatus }
  291.  
  292.  
  293.    Function WriteCommChar(OutChar : Char) : Boolean;
  294.       { Write a char out the comm port }
  295.  
  296.       Begin
  297.          WriteCommChar := __CallDOSIO(3, WriteChar, OutChar, 1);
  298.          end;  { Function WriteCommChar }
  299.  
  300.  
  301.    Procedure PrintChar(OutChar : Char);
  302.       { Print a character on the default printer }
  303.  
  304.       Begin
  305.          DummyLogical := __CallDOSIO(4, WriteChar, OutChar, 1);
  306.          End; { Procedure PrintCommChar }
  307.  
  308.  
  309.    Procedure WriteCommString(InString: STRINGLONG);
  310.       { Send a string of characters out the comm port }
  311.       { I use this for sending text file to the host  }
  312.  
  313.       Var
  314.          Counter : Byte;
  315.  
  316.       Begin
  317.          For Counter := 1 to Length(InString) Do
  318.             Begin
  319.                DummyLogical := WriteCommChar(InString[Counter]);
  320.                { Pause until you can send the next character out }
  321.                While Not CheckOutputStatus Do Delay(1);
  322.                End;  { Sending input keystroke }
  323.          End; { Procedure WriteCommString }
  324.  
  325.  
  326.    Function SendBreak : Boolean;
  327.       { Send a break character out the comm port for standardized }
  328.       { length of time. }
  329.  
  330.       Var
  331.          TempStatus : Boolean;
  332.  
  333.       Begin
  334.          TempStatus :=  __CallDOSIO(3, XMTBreak, Chr(0), 1);
  335.          Delay(250);
  336.          TempStatus := TempStatus And __CallDOSIO(3, StopBreak, Chr(0), 1);
  337.  
  338.          { Save total status }
  339.          SendBreak := TempStatus;
  340.  
  341.          end; { SendBreak }
  342.  
  343.  
  344.    Function InitPort : Boolean;
  345.       { Initialize communications port with default values }
  346.       Var
  347.          TempStatus : Boolean;
  348.  
  349.       Begin
  350.  
  351.          { First, we'll program the device with our default values. }
  352.  
  353. {         TempStatus := SetSerial(3,1200,1,DB7S,none,Chr(0));
  354.  
  355.          { Next, Enable Receiver Interrupts }
  356.          TempStatus := TempStatus And __CallDOSIO(3,EnableRecvIntr,Chr(0),1);
  357.  
  358.  
  359.          { Now, we'll tell it to set up the modem port for DTR, RTS & SRTS }
  360.          TempStatus := TempStatus And __CallDOSIO(3,SetModem,Chr(14),1);
  361.  
  362.          { Save total status }
  363.          InitPort := TempStatus;
  364.  
  365.          end; { InitPort }
  366.  
  367.  
  368.